﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class KHACHHANG_DAO
    {
        #region 1. Inserting
        public static bool ThemKhachHang(KHACHHANG_DTO kh)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@HoTen", kh.HoTen));
                sqlParams.Add(new SqlParameter("@DienThoai",kh.DienThoai));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("InsertKhachHang", sqlParams);

                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
         
        public static int GetMaKHByDienThoai(string sdt)
        {
            List<KHACHHANG_DTO> list = new List<KHACHHANG_DTO>();
            List<SqlParameter> sqlPara = new List<SqlParameter>();
            sqlPara.Add(new SqlParameter("@DienThoai",sdt));
            DataTable dt = SqlDataAccessHelper.ExecuteQuery("SelectKhachHangByDienThoai",sqlPara);
            if (dt != null && dt.Rows.Count > 0)
            {
                return int.Parse(dt.Rows[0][0].ToString());
            }
            return -1;  
        }
                   
/*
 *   foreach(DataRow dr in dt.Rows)
                {
                    KHACHHANG_DTO kh = new KHACHHANG_DTO();
                    kh.MaKH = int.Parse(dr["MaKH"].ToString());
                    kh.HoTen = dr["HoTen"].ToString();
                    kh.DienThoai = dr["DienThoai"].ToString();
                    list.Add(kh);

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;*/
       

    }
}
